﻿using System;
using System.ComponentModel;
using System.Globalization;
using System.Windows.Data;
using System.Windows.Input;
using BLL.Model;
using BLL.Model.Schema;
using BLL.ProjectManagement;
using GKS.Factory;

namespace GKS.Model.ViewModels
{
    public class AddEditProjectModel : INotifyPropertyChanged
    {
        public AddEditProjectModel()
        {
            CreateDate = DateTime.Today;
            Project = new Project();

            SaveButtonClicked = new SaveProject(this);
            CancelButtonClicked = new CancelProject(this);
        }

        public Project Project { get; set; }

        public string ProjectNameText
        {
            get
            {
                return Project.Name;
            }
            set
            {
                Project.Name = value;
            }
        }

        public string DescriptionText
        {
            get
            {
                return Project.Description;
            }
            set
            {
                Project.Description = value;
            }
        }

        public DateTime CreateDate
        {
            get { return Project.CreateDate; }
            set
            {
                Project.CreateDate = value;
            }
        }

        public ICommand SaveButtonClicked { get; set; }
        public ICommand CancelButtonClicked { get; set; }

        public event PropertyChangedEventHandler PropertyChanged;
    }

    public class SaveProject : ICommand
    {
        AddEditProjectModel _projectModel;
        ProjectManager _projectManger;

        public SaveProject(AddEditProjectModel projectModel)
        {
            _projectModel = projectModel;
            _projectManger = new ProjectManager(GKSFactory.GetProjectRepository(), GKSFactory.GetHeadRepository(), GKSFactory.GetRecordRepository());
        }

        public bool CanExecute(object parameter)
        {
            return true;
        }

        public event EventHandler CanExecuteChanged;

        public void Execute(object parameter)
        {
            _projectManger.AddOrUpdateProject(_projectModel.Project);
        }
    }

    public class CancelProject : ICommand
    {
        AddEditProjectModel _projectModel;
        public CancelProject(AddEditProjectModel projectModel)
        {
            _projectModel = projectModel;
        }

        public bool CanExecute(object parameter)
        {
            return true;
        }

        public event EventHandler CanExecuteChanged;

        public void Execute(object parameter)
        {
        }
    }

    public enum ProjectStatus
    {
        Active,
        Inactive
    }

    public class EnumConverter : IValueConverter
    {
        public object Convert(object value, Type targetType,
                              object parameter, CultureInfo culture)
        {
            if (value == null || parameter == null)
                return false;

            string checkValue = value.ToString();
            string targetValue = parameter.ToString();
            return checkValue.Equals(targetValue,
                     StringComparison.InvariantCultureIgnoreCase);
        }

        public object ConvertBack(object value, Type targetType,
                                  object parameter, CultureInfo culture)
        {
            if (value == null || parameter == null)
                return null;

            bool useValue = (bool)value;
            string targetValue = parameter.ToString();
            if (useValue)
                return Enum.Parse(targetType, targetValue);

            return null;
        }
    }
}
